package com.example.passwordmanager;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.Ohuang.ilivedata.LiveDataBus;
import com.Ohuang.ilivedata.MyLiveData;
import com.example.passwordmanager.databinding.FragmentPsdBinding;
import com.example.passwordmanager.db.AppDatabase;
import com.example.passwordmanager.db.UserBean;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


public class PsdFragment extends Fragment {
    private PsdActivity psdActivity;
    private FragmentPsdBinding fragmentPsdBinding;
    private View root;
    private ExecutorService executorService;
    private int id;


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        psdActivity = (PsdActivity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_psd, container, false);
        fragmentPsdBinding = FragmentPsdBinding.bind(root);
        executorService = Executors.newSingleThreadExecutor();
        initView();
        if (!psdActivity.isAdd()) {
            initEvent();
        }
        return root;
    }

    private void initView() {
        fragmentPsdBinding.btnBack.setOnClickListener(v -> {
            getActivity().finish();
        });

        fragmentPsdBinding.btnSave.setOnClickListener(v -> {
            saveFun();
        });
    }

    private void initEvent() {
        MyLiveData.getInstance().with("UserBean", UserBean.class).observe(getViewLifecycleOwner(), new Observer<UserBean>() {
            @Override
            public void onChanged(UserBean userBean) {
                id = userBean.getId();
                fragmentPsdBinding.etName.setText(userBean.getName());
                fragmentPsdBinding.etUsername.setText(userBean.getUsername());
                fragmentPsdBinding.etPassword.setText(userBean.getPassword());
                fragmentPsdBinding.etContent.setText(userBean.getContent());
                fragmentPsdBinding.etSecretKey.setText(userBean.getSecretKey());

            }
        });
    }

    private void saveFun() {
        UserBean userBean = new UserBean();
        userBean.setName(fragmentPsdBinding.etName.getText().toString());
        userBean.setUsername(fragmentPsdBinding.etUsername.getText().toString());
        userBean.setPassword(fragmentPsdBinding.etPassword.getText().toString());
        userBean.setSecretKey(fragmentPsdBinding.etSecretKey.getText().toString());
        userBean.setContent(fragmentPsdBinding.etContent.getText().toString());
        if (psdActivity.isAdd()) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    AppDatabase.getInstance(getActivity().getApplicationContext()).getUserDao().insertUserBean(userBean);
                }
            });
        } else {
            userBean.setId(id);
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    AppDatabase.getInstance(getActivity().getApplicationContext()).getUserDao().updateUserBean(userBean);
                }
            });
        }
        LiveDataBus.get().with("showDialog", String.class).setValue("操作成功");
        getActivity().finish();
    }
}